package com.sesame.mq.rabbit.topic.customer;

import com.rabbitmq.client.Channel;
import com.sesame.mq.rabbit.topic.config.RabbitTopicConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class RabbitTopicReciver {
    private static final Logger log = LoggerFactory.getLogger(RabbitTopicReciver.class);

    /**
     * ropic队列监听一.
     */
    @RabbitListener(queues = RabbitTopicConfig.USER_QUEUE_1)
    public void receiver1(String msg, Message message, Channel channel) throws IOException {
        System.err.println("1" + RabbitTopicConfig.USER_QUEUE_1 + ":" + msg);

        int i = 1;
        if (i > 0) {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } else {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }

    }

    /**
     * topic队列监听二.里异常需要处理
     */
    @RabbitListener(queues = RabbitTopicConfig.USER_QUEUE_2)
    public void receiver2(String msg, Message message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        //channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        //  channel.basicNack(tag, false,true);
        System.err.println("1" + RabbitTopicConfig.USER_QUEUE_2 + ":" + msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(queues = RabbitTopicConfig.USER_QUEUE_3)
    public void receiver3(String msg, Message message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) {
        //channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        //  channel.basicNack(tag, false,true);
        System.err.println("1" + RabbitTopicConfig.USER_QUEUE_3 + ":" + msg);
    }
}
